package com.zzx.threekingdom.card;

import java.util.ArrayList;
import java.util.Random;

public class CardList extends ArrayList<Card>
{
    public interface CardListListener
    {
        void onCardsChanged(CardList cards);
    };
    
    private static final long serialVersionUID = 1L;
    private CardListListener mListener;
    
    public void setListListener(CardListListener listener) { mListener = listener; }
    public void notiyListener()
    {
        if(mListener!=null)
            mListener.onCardsChanged(this);
    }
    

    public CardList shuffle()
    {
        Random randomObj = new Random();
        int num = size();
        for (int count = 0; count < num - 1; count++)
        {
            int position = randomObj.nextInt(num - 1);
            swap(count, position);
        }
        return this;
    }
        
    public void swap(int index1, int index2)
    {
        Card e1 = get(index1);
        Card e2 = get(index2);
        set(index1, e2);
        set(index2, e1);
    }
    @Override
    public boolean add(Card card)
    {
    	if(card==null)
    		return false;
        boolean bRet = super.add(card);
        notiyListener();
        return bRet;
    }
    public CardList addCard(Card card)
    {
    	if(card != null)
    		add(card);
    	return this;
    }
    
    public CardList addCards(CardList cards)
    {
        for(int i = 0;i < cards.size();i++)
        {
        	Card card = cards.get(i); 
        	if(card != null)
        		addCard(card);
        }
        notiyListener();
        return this;
    }
    
    public CardList drawCards(int num)
    {
        CardList cards = new CardList();
        int retNum = (size()>num)?num:size();
        for(int count = 0;count<retNum;count++)
        {
            cards.add(get(0));
            remove(0);
        }
        notiyListener();
        return cards;
    }
    
    public boolean drawCards(CardList cards)
    {
        boolean bRet = false;
        int num = cards.size();
        for(int count = 0;count<num;count++)
        {
            if(remove(cards.get(count))==true);
                bRet = true;
        }
        notiyListener();
        return bRet;
    }
    
    public CardList drawCard(Card card)
    {
        remove(card);
        notiyListener();
        return this;
    }
    
    public CardList getCards(int num)
    {
        CardList cards = new CardList();
        int retNum = (size()>num)?num:size();
        for(int count = 0;count<retNum;count++)
        {
            cards.add(get(count));
        }
        return cards;
    }
        
    public Object[] getObjectCards(int num)
    {
        return getCards(num).toArray();
    }
   
    public CardList setCards(CardList cards)
    {
        clear();
        addAll(cards);
        return this;
    }
    
    public CardList getUnionCards(CardList cards)
    {
        CardList retCards = new CardList();
        int num = cards.size();
        for(int count = 0;count<num;count++)
        {
            Card card = cards.get(count);
            if(contains(card)==true)
                retCards.add(card);
        }
        return retCards;
    }
    
    public static Card getFirstCard(CardList cards)
    {
        if(cards!=null&&cards.size()!=0)
            return cards.get(0);
        return null;
        
    }
    
    public static int getCardssCardNum(ArrayList<CardList> cardss)
    {
    	int ret = 0;
    	if(cardss != null)
    	{
    		for(int i = 0;i < cardss.size();i++)
    			ret += cardss.get(i).size();
    	}
    	return ret;
    }
    
    
    
    public boolean containsCards(CardList cards)
    {
        for(int i =0;i < cards.size();i++)
        {
            if(contains(cards.get(i))==false)
                return false;
        }
        return true;
    }
    



}
